﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using Newtonsoft.Json;

public partial class Account_Register : System.Web.UI.Page
{
    User user;
    protected void Page_Load(object sender, EventArgs e)
    {
        user = (User)Session["UserSession"];
        
        JsonParser jsonParser = new JsonParser();
        try
        {
            Debug.WriteLine(user.username);
            if (user != null)
            {
                Response.Redirect("~/Account/Profile.aspx");
            }

        }
        catch (Exception ex)
        {

        }
    }


    protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        User user = createUser();
        if (user != null)
        {
            string jsonString = user.serialize();
            JsonParser jsonParser = new JsonParser();
            jsonParser.registerUser(jsonString);

            FormsAuthentication.SetAuthCookie(TxtUserName.Text, false /* createPersistentCookie */);
            string continueUrl = "~/Account/NewTeam.aspx";
            Session["UserSession"] = user;
            Response.Redirect(continueUrl);
        }
    }

    private User createUser()
    {
        User user = null;
        if (TxtPassword.Text == TxtRePassword.Text)
        {
            user = new User();
            user.name = TxtUserName.Text;
            user.username = TxtUserName.Text;
            user.password = TxtPassword.Text;
            user.mail = TxtMail.Text;
        }
        else
        {
            Response.Write("Password did not match");
            TxtPassword.Focus();
        }
        return user;
    }

}
